import JSEncrypt from 'jsencrypt';
const NodeRSA = require('node-rsa')

export default {
  // 加密
  encrypt(publicKey,txt) {
    const encryptor = new JSEncrypt()
    encryptor.setPublicKey(publicKey) // 设置公钥
    return encryptor.encrypt(txt) // 对需要加密的数据进行加密
  },
  // 解密
  decrypt(publicKey,txt) {
    let pk = '-----BEGIN PUBLIC KEY-----\n' +publicKey + '\n-----END PUBLIC KEY-----'
    try {
      const nodersa2 = new NodeRSA(pk);
    return  nodersa2.decryptPublic(txt, "utf8");

    } catch (error) {
      return ''
    }



  }
}

// export function encrypt(publicKey,txt) {
//   const encryptor = new JSEncrypt()
//   encryptor.setPublicKey(publicKey) // 设置公钥
//   return encryptor.encrypt(txt) // 对需要加密的数据进行加密
// }
// export function decrypt(publicKey,txt) {
//   const encryptor = new JSEncrypt()
//   encryptor.setPublicKey(publicKey)
//   return encryptor.decrypt(txt)
// }
